package logic

import (
	"context"
	"github.com/jinzhu/copier"
	"github.com/zeromicro/go-zero/core/logx"
	"grpc-common/search/types/search"
	"listenboos-common/kafka/model"
	"search/domain"
	"search/internal/svc"
	"strconv"
)

type SearchLogic struct {
	ctx          context.Context
	svcCtx       *svc.ServiceContext
	searchDomain *domain.SearchDomain
	logx.Logger
}

func (l SearchLogic) OnSaleAlbum(in *search.OnSaleAlbumReq) (*search.OnSaleAlbumResp, error) {
	var onSaleAlbumInfo model.AlbumInfo
	err := copier.Copy(&onSaleAlbumInfo, in)
	if err != nil {
		return nil, err
	}
	err = l.searchDomain.OnOnSaleAlbum(l.ctx, onSaleAlbumInfo)
	if err != nil {
		return nil, err
	}
	return nil, nil
}

func (l SearchLogic) GetAlbumPropertyValueInfoByTitle(in *search.GetAlbumInfoByIdReq) (*search.GetAlbumInfoByIdResp, error) {
	keyWords := []string{in.KeyWord}
	infos, err := l.searchDomain.Search(l.ctx, keyWords)
	if err != nil {
		return nil, err
	}
	return &search.GetAlbumInfoByIdResp{SearchAlbumInfo: infos}, nil
}

func (l SearchLogic) DeleteAlbum(in *search.DeleteAlbumReq) (*search.DeleteAlbumResp, error) {
	return nil, nil
}

func (l SearchLogic) GetAlbumChannel(in *search.GetAlbumChannelReq) (*search.GetAlbumChannelResp, error) {
	keyWords := []string{in.KeyWord}
	infos, err := l.searchDomain.SearchChannel(l.ctx, keyWords)
	if err != nil {
		return nil, err
	}
	return &search.GetAlbumChannelResp{SearchAlbumInfo: infos}, nil
}

func (l SearchLogic) SearchAlbumInfo(in *search.AlbumInfoReq) (*search.AlbumInfoResp, error) {
	infos, total, totalPages, err := l.searchDomain.SearchAlbumInfo(l.ctx, in)
	if err != nil {
		return nil, err
	}
	return &search.AlbumInfoResp{AlbumInfoList: infos,
		PageNo:     in.PageNo,
		PageSize:   in.PageSize,
		Total:      total,
		TotalPages: totalPages,
	}, nil
}

// TODO:未完成
func (l SearchLogic) KeyWord(in *search.KeywordReq) (*search.KeywordResp, error) {
	infos, err := l.searchDomain.SuggestSearch(l.ctx, in.Keyword)
	if err != nil {
		return nil, err
	}
	return &search.KeywordResp{Keywords: infos}, nil
}

func (l SearchLogic) FindAlbumInfo(in *search.FindAlbumInfoReq) (*search.AlbumInfo, error) {
	albumId := strconv.Itoa(int(in.AlbumId))
	info, err := l.searchDomain.FindAlbumInfo(l.ctx, albumId)
	if err != nil {
		return nil, err
	}
	return info, nil
}

func (l SearchLogic) UpdateRanking(in *search.UpdateRankingReq) (*search.UpdateRankingResp, error) {
	albumInfos, err := l.searchDomain.UpdateRanking(l.ctx, in.Category1Id, in.Dimension)
	if err != nil {
		return nil, err
	}
	var albumList []*search.AlbumInfoV1
	err = copier.Copy(&albumList, &albumInfos)
	if err != nil {
		return nil, err
	}
	return &search.UpdateRankingResp{
		AlbumList: albumList,
	}, nil
}

func NewSearchLogicLogic(ctx context.Context, svcCtx *svc.ServiceContext) *SearchLogic {
	return &SearchLogic{
		ctx:          ctx,
		searchDomain: domain.NewSearchDomain(),
		svcCtx:       svcCtx,
	}
}

//func (l *LiveLogic) GetAllTag(in *live.GetAllTagReq) (*live.GetAllTagResp, error) {
//	tags, err := l.liveTagDomain.GetAllTag()
//	if err != nil {
//		return nil, err
//	}
//	var res []*live.Tag
//	err = copier.Copy(&res, &tags)
//	if err != nil {
//		return nil, err
//	}
//	return &live.GetAllTagResp{
//		Tags: res,
//	}, nil
//}
//
//func (l *LiveLogic) GetCurrentLive(in *live.GetCurrentLiveReq) (*live.GetCurrentLiveResp, error) {
//	// 先查询用户直播间是否已经存在
//	ctx, canal := context.WithTimeout(context.Background(), 10*time.Minute)
//	defer canal()
//	room, err := l.liveRoomDomain.FindRoomByUserId(ctx, in.UserId)
//	if room == nil {
//		return nil, err
//	}
//	if room.Id == 0 {
//		return nil, nil
//	}
//	// 如果存在查询用户标签
//	tagId, err := strconv.ParseInt(room.TagId, 10, 64)
//	if err != nil {
//		return nil, err
//	}
//	tag, err := l.liveTagDomain.FindTagById(ctx, tagId)
//	if err != nil {
//		return nil, err
//	}
//	var res live.GetCurrentLiveResp
//	err = copier.Copy(&res, &room)
//	if err != nil {
//		return nil, err
//	}
//	var t live.Tag
//	err = copier.Copy(&t, &tag)
//	if err != nil {
//		return nil, err
//	}
//	res.Tags = append(res.Tags, &t)
//	return &res, nil
//}
